import { defineStore } from "pinia";
import { computed, ref } from "vue";

// 组合式API, 以对象和封装的视角管理

let useTodoStore = defineStore('todo', () => {
  // 声明为响应式数据才能纳入状态管理
  let todos = ref([
    {id: 1, title: '吃饭'},
    {id: 2, title: '吃饭2'},
    {id: 3, title: '吃饭3'},
  ]);

  let arr = ref([1,2,3]);

  let total = computed(() => {
    return arr.value.reduce((prev, next) => {
      return prev + next;
    }, 0);
  })

  return {
    todos,
    arr: arr,
    addTodo: () => {
      todos.value.push({id: 4, title: '吃饭4'});
    },
    total
  }
});

export default useTodoStore;